﻿using System;
using System.Collections.Generic;
using System.Linq; 
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.SPI;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataModel.Plugin
{
    /// <summary>
    /// Database MSSQL Builder
    /// </summary>
    public class DatabaseMSSQLBuilder : IMetaBusinessSQLBuilder
    {
        private static readonly string DBDefSQL = "CREATE DATABASE {0};";

        public IList<IMetaDataContent> Contents { get; set; }


        public List<SQLItem> BuildBusinessSQL(object entiy)
        {
            Database db = entiy as Database;
            if (db == null || db.DatabaseConfigs == null || db.DatabaseConfigs.Count == 0)
                throw new ArgumentException("生成Database元数据SQL失败！检查检查参数");
            SQLItem item = new SQLItem
            {
                SQLType = SQLType.DDL,
                ConfigKey = db.DatabaseConfigs.Find(cfg => cfg.IsDefault).ConfigKey,
                DatabaseType = (DatabaseType)Enum.Parse(typeof(DatabaseType), db.DbType),
                IsCanRepeat = false,
                SQL = string.Format(DBDefSQL, db.Code),
            };
            return new List<SQLItem> { item };
        }

        public List<SQLItem> BuildPresetDataSQL(object entity)
        {
            throw new NotImplementedException();
        }
    }
}
